package com.ruoyi.web.controller.channel;

import com.ruoyi.basedata.domain.Bank;
import com.ruoyi.basedata.service.IBankService;
import com.ruoyi.channel.domain.ChannelWalletRSP;
import com.ruoyi.channel.domain.ChannelWithdrawalBank;
import com.ruoyi.channel.domain.ChannelWithdrawalBankRSP;
import com.ruoyi.channel.domain.MpChannel;
import com.ruoyi.channel.service.IChannelWithdrawalBankService;
import com.ruoyi.channel.service.IMpChannelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-08
 */
@Controller
@RequestMapping("/channel/channelWithdrawalBank")
public class ChannelWithdrawalBankController extends BaseController
{
    private String prefix = "channel/channelWithdrawalBank";
	
	@Autowired
	private IChannelWithdrawalBankService channelWithdrawalBankService;
	@Autowired
	private IMpChannelService iMpChannelService;
	@Autowired
	private IBankService bankService;
	
	@GetMapping()
	public String channelWithdrawalBank()
	{
	    return prefix + "/channelWithdrawalBank";
	}
	
	/**
	 * 查询列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ChannelWithdrawalBank channelWithdrawalBank)
	{
		startPage();
        List<ChannelWithdrawalBank> list = channelWithdrawalBankService.selectChannelWithdrawalBankList(channelWithdrawalBank);
        List<ChannelWithdrawalBankRSP> listRSP = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			ChannelWithdrawalBankRSP channelWithdrawalBankRSP = new ChannelWithdrawalBankRSP();
			ChannelWithdrawalBank withdrawalBank = list.get(i);
			// 渠道-代付银行id
			channelWithdrawalBankRSP.setId(withdrawalBank.getId());
			// 渠道名称
			MpChannel mpChannelById = iMpChannelService.selectMpChannelById(withdrawalBank.getChannelId().intValue());
			channelWithdrawalBankRSP.setChannelName(mpChannelById.getName());
			// 渠道标识
			channelWithdrawalBankRSP.setChannelId(withdrawalBank.getChannelId());
			// 渠道编码
			channelWithdrawalBankRSP.setChannelCode(mpChannelById.getCode());
			// 银行类型/名称
			Bank bank = bankService.selectBankById(withdrawalBank.getBankId().intValue());
			channelWithdrawalBankRSP.setBankName(bank.getName());
			// 银行标识
			channelWithdrawalBankRSP.setBankId(withdrawalBank.getBankId());
			// 银行编码
			channelWithdrawalBankRSP.setBankCode(bank.getCode());

			listRSP.add(channelWithdrawalBankRSP);
		}
		return getDataTable(listRSP);
	}
	
	
	/**
	 * 导出列表
	 */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChannelWithdrawalBank channelWithdrawalBank)
    {
    	List<ChannelWithdrawalBank> list = channelWithdrawalBankService.selectChannelWithdrawalBankList(channelWithdrawalBank);
		List<ChannelWithdrawalBankRSP> listRSP = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			ChannelWithdrawalBankRSP channelWithdrawalBankRSP = new ChannelWithdrawalBankRSP();
			ChannelWithdrawalBank withdrawalBank = list.get(i);
			// 渠道-代付银行id
			channelWithdrawalBankRSP.setId(withdrawalBank.getId());
			// 渠道名称
			MpChannel mpChannelById = iMpChannelService.selectMpChannelById(withdrawalBank.getChannelId().intValue());
			channelWithdrawalBankRSP.setChannelName(mpChannelById.getName());
			// 渠道标识
			channelWithdrawalBankRSP.setChannelId(withdrawalBank.getChannelId());
			// 银行类型/名称
			Bank bank = bankService.selectBankById(withdrawalBank.getBankId().intValue());
			channelWithdrawalBankRSP.setBankName(bank.getName());
			// 银行标识
			channelWithdrawalBankRSP.setBankId(withdrawalBank.getBankId());

			listRSP.add(channelWithdrawalBankRSP);
		}
        ExcelUtil<ChannelWithdrawalBankRSP> util = new ExcelUtil<ChannelWithdrawalBankRSP>(ChannelWithdrawalBankRSP.class);
        return util.exportExcel(listRSP, "channelWithdrawalBank");
    }
	
	/**
	 * 新增
	 */
	@GetMapping("/add/{channelId}")
	public String add(@PathVariable("channelId") Integer channelId, ModelMap mmap)
	{
		// 查询渠道已添加的银行，返回未添加的银行
		List<ChannelWithdrawalBank> exisChannelWithdrawalBankList = channelWithdrawalBankService.selectChannelWithdrawalBankListByChannelId(channelId);
		if (exisChannelWithdrawalBankList.size() > 0) {
			List<Integer> bankIds = new ArrayList<>();
			for (ChannelWithdrawalBank channelWithdrawalBank : exisChannelWithdrawalBankList) {
				bankIds.add(channelWithdrawalBank.getBankId().intValue());
			}
			mmap.put("banks",bankService.selectBankListWithoutBankIds(bankIds));
		}else {
			// 渠道未添加任何银行类型，返回所有
			Bank bank = new Bank();
			mmap.put("banks",bankService.selectBankList(bank));
		}
		mmap.put("mpChannel",iMpChannelService.selectMpChannelById(channelId));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@Log(title = "渠道-代付银行", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Long channelId, Long[] bankIds)
	{		
		return channelWithdrawalBankService.insertChannelWithdrawalBankByChannelIdAndBankIds(channelId, bankIds);
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		ChannelWithdrawalBank channelWithdrawalBank = channelWithdrawalBankService.selectChannelWithdrawalBankById(id);
		mmap.put("channelWithdrawalBank", channelWithdrawalBank);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ChannelWithdrawalBank channelWithdrawalBank)
	{		
		return toAjax(channelWithdrawalBankService.updateChannelWithdrawalBank(channelWithdrawalBank));
	}
	
	/**
	 * 删除
	 */
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(channelWithdrawalBankService.deleteChannelWithdrawalBankByIds(ids));
	}
	
}
